<?php
/**
 * Created by PhpStorm.
 * User: zxwwjs@hotmail.com
 * Date: 2017-6-28
 * Time: 21:21
 */

namespace App\Repositories;

use App\Repositories\Contracts\UserRepositoryInterface;
use App\Repositories\BaseInterface\Repository;

class UserRepository extends Repository implements UserRepositoryInterface {

  public function model() {
    return "App\Models\User";
  }

  /**
   * @param int $perPage
   * @param array $columns
   *
   * @return mixed
   */
  public function paginate( $perPage = 25, $columns = array( '*' ) ) {
    $query = $this->model->where( "name", "<>", 'jing.min@163.com' );

    return $query->paginate( $perPage, $columns );
  }

  /**
   * @param int $id
   * @param string $password
   *
   * @return boolean
   */
  public function changePassword( $id, $password ) {
    return $this->update( [ 'password' => bcrypt( $password ) ], $id );

  }

  /**
   * @param array $data
   * @param $id
   *
   * @return mixed
   */

  public function updateRich( array $data, $id ) {
    if ( $data['password'] ) {
      $data['password'] = bcrypt( $data['password'] );
    } else {
      unset( $data['password'] );
    }
    return parent::updateRich( $data, $id );
  }

  /**
   * @param array $data
   *
   * @return mixed
   */
  public function create( array $data ) {
    $data['password'] = bcrypt( $data['password'] );
    return parent::create( $data );
  }

  /**
   *
   * @param integer $userId
   * @param integer $attachmentId
   *
   * @return mixed|null
   */

  public function setAvatar($userId,$attachmentId){
    $user =$this->find($userId);
    if(!$user){
      return null;
    }
    $user->avatar()->sync([$attachmentId]);
    $user->load("avatar");
    return $user->toArray();

  }


}
